package com.geese.common.vo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.geese.common.constants.Constants;
import com.github.pagehelper.Page;
import com.google.common.collect.Maps;

public class Paginator<E> {

	private long pageSize;
	private long pageNumber;
	private long pageTotal;
	private long total;
	private List<E> results = new ArrayList<>();
	
	public Paginator(Page page, List<E> result) {
		this.pageSize = page.getPageSize();
		this.pageNumber = page.getPageNum();
		this.pageTotal = page.getPages();
		this.total = page.getTotal();
		this.results = result;
	}
	
	public long getPageSize() {
		return pageSize;
	}
	
	public long getPageNumber() {
		return pageNumber;
	}

	public long getPageTotal() {
		return pageTotal;
	}

	public Paginator<E> setPageTotal(long pageTotal) {
		this.pageTotal = pageTotal;
		return this;
	}

	public long getTotal() {
		return total;
	}

	public List<E> getResults() {
		return results;
	}

	public Paginator<E> setResults(List<E> results) {
		this.results = results;
		return this;
	}

	public Paginator<E> addResult(E result) {
		this.results.add(result);
		return this;
	}
	
	public boolean isEmpty() {
		return this.results.isEmpty();
	}
	
	public String toString() {
		return String.format("{page: {size: %d, number: %d, total: %d}", pageSize, pageNumber, pageTotal, total);
	}
	
	public <T> Map<String, Object> getPageResult() {
		Map<String, Object> result = Maps.newHashMap();
		result.put(Constants.CURR_PAGE, this.pageNumber);
		result.put(Constants.TOTAL_PAGE, this.pageTotal);
		result.put(Constants.TOTAL, this.total);
		return result;
	}
	
}
